
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Text;

namespace PractBTS.ErrorCoolection.HelperClasses
{
    /// <summary>
    /// Template to Serialize to and from XML
    /// </summary>
    /// <typeparam name="T"></typeparam>
        public class ObjectXMLSerializer<T> where T: class
        {
                /// <summary>
                /// Serializes the object to a XML string
                /// <see cref="BinaryFormatter" />.
                /// </summary>
                /// <param name="obj">The object to Serialize.</param>
                /// <remarks>
                /// The object tomust be XML serializable.
                /// </remarks>
                public  string ToXMLString(T obj)
                {
                        using (StringWriter buffer = new StringWriter())
                        {
                                XmlSerializer ser = new XmlSerializer(typeof(T));
                                ser.Serialize(buffer, obj); 
                                return buffer.ToString();
                        }
                }
                /// <summary>
                /// Serializes the object to a XML string
                /// <see cref="BinaryFormatter" />.
                /// </summary>
                /// <param name="obj">The object to Serialize.</param>
                /// <remarks>
                /// The object tomust be XML serializable.
                /// </remarks>
                public T FromXMLString(string str)
                {
                        using (StringReader buffer = new StringReader(str))
                        {
                                XmlSerializer ser = new XmlSerializer(typeof(T));
                                T obj = ser.Deserialize(buffer) as T;
                                return obj;
                        }
                }

        }
}
